import { GraphNode,GraphEdge,Graph } from "../model/graph";

export class NodeInput {
    id: string;
    name: string;
    attrs: object = {};
    constructor(id: string, name: string, attrs: object) {
        this.id = id;
        this.name = name;
        this.attrs = attrs;
    }
}

export class EdgeInput {
    source: string;
    target: string;
    constructor(source: string, target: string) {
        this.source = source;
        this.target = target;
    }
}

export function buildGraph(nodes: NodeInput[], edges: EdgeInput[]): Graph {
    const graph = new Graph();
    const nodeMap = new Map<string, GraphNode>();
    for (const node of nodes) {
        const graphNode = new GraphNode(node.id, node.name, node.attrs, [], []);
        graph.addNode(graphNode);
        nodeMap.set(node.id, graphNode);
    }
    for (const edge of edges) {
        const sourceNode = nodeMap.get(edge.source) as GraphNode;
        const targetNode = nodeMap.get(edge.target) as GraphNode;
        const graphEdge = new GraphEdge(sourceNode, targetNode);
        graph.addEdge(graphEdge);
        sourceNode.outgoingEdges.push(graphEdge);
        targetNode.incomingEdges.push(graphEdge);
    }
    return graph;
}
